; altmenu.asm
; alternate menu bar

	.include "common.inc"
	.include "global.inc"
	.include "msgbox.inc"
	.include "screen.inc"
	.include "window.inc"
	.include "wutil.inc"
	.include "execfun.inc"
	.include "listbox.inc"
	.include "string.inc"
	.include "memory.inc"

	.export menu_init

	WIN_SIZE_X = 144
	WIN_SIZE_Y = 112
	BORDER_OX = 4
	BORDER_OY = 4
	BUTTON_DX = 16
	BUTTON_DY = 12

	.segment "RODATA"

txtitle:	.byte 19, "Dynamic memory info"
txmemtot:	.byte  8, 13, "Total: "
txmemfre:	.byte  7, 13, "Free: "
txmemblk:	.byte  8, 13, "Block: "	

	.segment "DATA"
	
curfrm:		.byte 0
curwin:		.byte 0
curbox:		.byte 0

	.segment "CODE"

menu_init:	jsr lbtest

			lda #250				; init msg buf
			sta scratchbuf1
			lda #<scratchbuf1
			ldx #>scratchbuf1
			jsr ssetbuf
			jsr sclear				; add title
			lda #<txtitle
			ldx #>txtitle
			jsr saddstr
			
			lda #<txmemtot
			ldx #>txmemtot
			jsr saddstr
			jsr mgettotal
			jsr sputuint

			lda #<txmemfre
			ldx #>txmemfre
			jsr saddstr
			jsr mgetfree
			jsr sputuint

			lda #<txmemblk
			ldx #>txmemblk
			jsr saddstr
			jsr mgetmax
			jsr sputuint
			
			lda #<(scratchbuf1+1)	; open message box 
			ldx #>(scratchbuf1+1)
			ldy #MSG_INFO
			jsr message
			rts

;
;	Open test list box
;

lbtest:		; create frame
			ldx	#WIN_SIZE_X
			ldy #WIN_SIZE_Y
			jsr frm_create
			sta curfrm
			jsr frm_center
			; init main window
			jsr frm_getwin
			sta curwin
			jsr win_realize
			lda #WSTYLE_BCKGND|WSTYLE_HASTITLE|WSTYLE_BORDER
			jsr win_setstyle
			lda #1			; lower case system font
			jsr win_setfont
			lda #<lbtitle
			ldx #>lbtitle
			jsr win_settext
			lda curwin
			jsr addclsbtn
			
			; init listbox
			lda curwin
			ldx #<lbcreatest
			ldy #>lbcreatest
			jsr lb_create
			sta curbox
			
			; set lb lines
			ldx #8
			jsr lb_setcnt

	BUTTON_WIDTH = 6*8

			; create button
			lda curwin
			jsr win_create
			lda #WSTYLE_BORDER
			jsr win_setstyle
			ldx #(WIN_SIZE_X - BUTTON_WIDTH)/2
			ldy #WIN_SIZE_Y-BORDER_OY-BUTTON_DY
			jsr win_setpos
			ldx #BUTTON_WIDTH
			ldy #BUTTON_DY
			jsr win_setdim
			lda #<txapply
			ldx #>txapply
			jsr win_settext
			lda #WTYPE_BUTTON
			jsr win_settype
			lda #<ex_apply_button
			ldx #>ex_apply_button
			jsr win_setexec
			

			; draw window			
			lda curwin
			jsr win_draw
			lda curwin
			jsr win_show			; to screen
			
			rts			

;
;	Applies color selection to screen
;

ex_apply_button:
			jsr btncheck
			lda curbox
			jsr lb_getpos
			pha
			and #1
			eor #1
			jsr setfgcolor
			pla
			jmp setbkcolor

;
;	Gets item text for list box
;	INPUT:
;		X ... column
;		Y ... line
;	OUTPUT
;		A/X ... ptr to text
;

get_text2:	iny
			tya
			and #1
			tay
get_text1:	lda txcolorlo, y
			ldx txcolorhi, y
			rts

	.segment "DATA"

col:		.byte 0
line:		.byte 0

	TEXTBUF_SIZE = 10

textbuf:	.byte TEXTBUF_SIZE, 0
			.res TEXTBUF_SIZE

	.segment "RODATA"

color0:		.byte 5, "black"
color1:		.byte 5, "white"
color2:		.byte 3, "red"
color3:		.byte 4, "cyan"
color4:		.byte 6, "purple"
color5:		.byte 5, "green"
color6:		.byte 4, "blue"
color7:		.byte 6, "yellow"

txapply:	.byte 5, "apply"

txcolorlo:	.byte <color0, <color1, <color2, <color3
			.byte <color4, <color5, <color6, <color7
txcolorhi:	.byte >color0, >color1, >color2, >color3
			.byte >color4, >color5, >color6, >color7

; LbCreateStruct for directory listbox
lbcreatest:	.byte BORDER_OX ; left
			.byte BORDER_OY+WIN_TITLE_HEIGHT ; top
			.byte WIN_SIZE_X-2*BORDER_OX ; width
			.byte WIN_SIZE_Y-3*BORDER_OY-BUTTON_DY-WIN_TITLE_HEIGHT ; height
			.byte 2 ; font
			.byte 2 ; columns
; column 1
			.byte 12*4	; width
			.word col1title ; title
			.byte WSTYLE_TXLEFT ; txpos_title
			.byte WSTYLE_TXLEFT ; txpos_item
			.word get_text1
; column 2
			.byte 12*4	; width
			.word col2title ; title
			.byte WSTYLE_TXLEFT ; txpos_title
			.byte WSTYLE_TXLEFT ; txpos_item
			.word get_text2

col1title:	.byte 10, $42, $01, $03, $0B, $07, $12, $0F, $15, $0E, $04 
			; "Background"
col2title:	.byte 10, $46, $0F, $12, $05, $07, $12, $0F, $15, $0E, $04
			; "Foreground"

lbtitle:	.byte 12, "setup colors"

